package com.group3.group3.post.control;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Tom
 * @Date: 2022/05/19/22:30
 * @Description:
 */

/**
 *@ClassName PostController
 *@Description
 *@Author 王琛
 *@Date 2022/5/19 22:30
 @Version 1.0
 */


import com.alibaba.fastjson.JSON;
import com.group3.group3.common.control.BaseController;
import com.group3.group3.mybatis.entity.PtDutyEntity;
import com.group3.group3.mybatis.flexigrid.PadingRstType;
import com.group3.group3.mybatis.flexigrid.PagingBean;
import com.group3.group3.mybatis.ztree.ZtreeEntity;
import com.group3.group3.post.entity.SearchPostEntity;
import com.group3.group3.post.service.PostService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


@Controller
@RequestMapping("/post")
public class PostController {

    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private PostService postService;
    @Secured("ROLE_gqgy_data_post")
    @RequestMapping("/page")
    public String menuPage(){
        return "post/page";
    }
    @Secured("ROLE_gqgy_data_post")

    @RequestMapping("/getPostTree")
    @ResponseBody
    public List<ZtreeEntity> getPostTree(){
        return postService.getPostTree();

    }
    @RequestMapping("/getPostInfoListByPage")//分页
    @ResponseBody
    public PadingRstType<PtDutyEntity> getPostInfoListByPage(SearchPostEntity search, PagingBean paging){
        logger.info(search);
        PadingRstType<PtDutyEntity> ptPostEntityPadingRstType = postService.getPostInfoListByPage(search,paging);
        return ptPostEntityPadingRstType;
    }

    @Secured("ROLE_gqgy_data_post")
    @RequestMapping("/getPostInfoById")
    @ResponseBody
    public PtDutyEntity getPostInfoById(@RequestParam("postId") String postId){
        logger.info("postId:" + postId);
        return postService.getPostInfoById(postId);
    }

    @Resource
    private MessageSource messageSource;

    @RequestMapping("/deletePostById")//分页
    @ResponseBody
    public String deletePostById(@RequestParam("postId")String postId){
        logger.info("postId"+postId);
        Map<String,Object> rstMap=new HashMap<>();
        Locale locale= LocaleContextHolder.getLocale();
        String msgStr=messageSource.getMessage("post.delete.success",null,locale);
        rstMap.put("success",true);
        rstMap.put("msg",msgStr);
        return JSON.toJSONString(rstMap);
    }

}
